/********************************************************************************
 * Project Name		[JavaEE_JDBC]
 * File Name     	[UserDaoImpl.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package dao.impl3;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import bean.User;
import dao.UserDao;

/**
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class UserDaoImpl implements UserDao, ResultSetHandler {

	private JdbcTemplate template = new JdbcTemplate();

	/**
	 * 根据结果集来组装对象
	 * 
	 * @param resultSet
	 * @return
	 * @throws SQLException
	 */
	@Override
	public Object handleResultSet(ResultSet resultSet) throws SQLException {
		User user = new User();
		user.setUsername(resultSet.getString("username"));
		user.setPassword(resultSet.getString("password"));
		user.setAge(resultSet.getInt("age"));
		user.setGender(resultSet.getString("gender"));
		user.setBirthday(new java.util.Date(resultSet.getDate("birthday").getTime()));
		user.setImage(resultSet.getBlob("image"));
		user.setDescription(resultSet.getString("description"));
		return user;
	}

	/**
	 * 添加用户
	 * 
	 * @param user
	 * @return
	 */
	@Override
	public boolean addUser(User user) {
		if (this.findUserByName(user.getUsername()) != null) {
			throw new RuntimeException("用户已存在");
		}
		String sql = "INSERT INTO user(username, password, age, gender, birthday, image, description) VALUES(?, ?, ?, ?, ?, ?, ?)";
		Object[] parameters = { user.getUsername(), user.getPassword(), user.getAge(), user.getGender(),
				user.getBirthday(), null, user.getDescription() };
		return template.save(sql, parameters) != 0;
	}

	/**
	 * 根据用户名查找用户
	 * 
	 * @param username
	 * @return
	 */
	@Override
	public User findUserByName(String username) {
		User user = null;

		String sql = "SELECT * FROM User where username = ?";
		Object[] parameters = { username };
		List<Object> users = template.find(sql, this, parameters);
		if (users != null && users.size() > 0) {
			user = (User) users.get(0);
		}

		return user;
	}

	/**
	 * 更新用户
	 * 
	 * @param user
	 * @param username
	 * @return
	 */
	@Override
	public boolean updateUserByName(User user, String username) {
		if (this.findUserByName(username) == null) {
			throw new RuntimeException("要更新的用户不存在");
		}

		String sql = "UPDATE User SET username = ?, password = ?, age = ?, gender = ?, birthday = ?, description = ? WHERE username = ?";
		Object[] parameters = { user.getUsername(), user.getPassword(), user.getAge(), user.getGender(),
				user.getBirthday(), user.getDescription(), username };
		return template.update(sql, parameters) != 0;
	}

	/**
	 * 根据用户名删除用户
	 * 
	 * @param username
	 * @return
	 */
	@Override
	public boolean deleteUserByName(String username) {
		if (this.findUserByName(username) == null) {
			throw new RuntimeException("要删除的用户不存在");
		}

		String sql = "DELETE FROM User where username = ?";
		Object[] parameters = { username };
		return template.delete(sql, parameters) != 0;
	}

	/**
	 * 根据用户名和密码登陆
	 * 
	 * @param username
	 * @param password
	 * @return
	 */
	@Override
	public boolean login(String username, String password) {
		User user = this.findUserByName(username);
		if (user == null) {
			throw new RuntimeException("用户名不正确");
		} else {
			if (!password.equals(user.getPassword())) {
				throw new RuntimeException("密码不正确");
			}
		}
		return true;
	}
}
